package com.wlr.study.leetcode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class LeetCode151 {


    public String test01(String s){
        int j=s.length()-1;
        StringBuilder sb = new StringBuilder();
        for (int i = s.length() - 1; i >= 0; i--) {
            if(i>=1){
                if(s.charAt(i)==' '&&s.charAt(i-1)!=' '){
                    String temp = s.substring(i,j).trim();
                    sb.append(temp).append(" ");
                    j=i;
                }
            }

        }
        return sb.toString().trim();
    }
    public String reverseWords(String s) {
        StringBuilder stringBuilder = new StringBuilder();
        String[] s1 = s.split(" ");
        for (int i = s1.length - 1; i >= 0; i--) {
            if (!s1[i].contains(" ") && !s1[i].isEmpty()) {
                stringBuilder.append(s1[i]);
                stringBuilder.append(" ");
            }
        }
        return stringBuilder.toString().trim();
    }

    public String reverseWords1(String s) {
        StringBuilder sb = new StringBuilder();
        String[] s1 = s.split(" ");
        int j = 0;
        for (int i = s1.length - 1; i > j; i--) {
//            if (!s1[i].contains(" ") && !s1[i].isEmpty()) {
                String temp = s1[i];
                s1[i] = s1[j];
                s1[j] = temp;
                j++;
//            }
        }
        for (int i = 0; i < s1.length; i++) {

        }
        return Arrays.toString(s1);
    }

    public static void main(String[] args) {
        LeetCode151 leetCode151 = new LeetCode151();
        System.out.println(leetCode151.reverseWords1("a good   example"));
    }
}
